<?php
namespace Home\Controller;
use Boris\DumpInspector;
use Think\Controller;
class UserController extends BaseController {

	/**
	* @desc: 用户登录接口
	* @Author: quyang
    * @again quyang 2017-1-17
	* @update: 2016-11-22
	**/
	public function UserInfoAction(){
		$data=I('param.');
		if(empty($data['userName']) || empty($data['userPwd'])){
			$this->param_error();
		}

		$User = new \Home\Model\UserModel();
		//验证用户名
        $userName = $User->where(array('f_user_name'=>$data['userName']))->field('f_user_id')->find();

        empty($userName) && $this->auto_error('用户不存在！');

		$where = array('f_user_name'=>$data['userName'],'f_user_pwd'=>$data['userPwd']);

		$userInfo = $User->userlogin($where);

//        var_dump($userInfo);die;
		if ($userInfo == false){
		    $this->auto_error('密码错误！');
        }

        //兼容享买   享买登录tag值为  ： xiangmai
        if ( empty( $data['tag'] ) )
        {
            if ( $userInfo['fUserStatus'] == 0 ){
                $this->auto_error('用户已禁用，请联系上级代理');
            }

            if ( ($userInfo['fUserTypeId'] !=0 && $userInfo['fUserTypeId'] !=1 ) || ( $userInfo['fJurisdictionType'] !=0 && $userInfo['fJurisdictionType'] !=2 ) ){
                $this->auto_error('此用户非站点和快递员');
            }

            //get参数
            $getData = I('get.');
//            dump($getData);die;

            //安卓版本 >= 32 普通版和专业版合并，登录没有限制
            if ( $getData['bmxSource'] == 'android' )
            {

                //版本信息为空或版本信息<32 则为老版本app 需正常验证身份
                if ( $getData['buildVersion'] < '32' || empty( $getData['buildVersion'] ) )
                {

                    //  如果是专业版的APP, 但不是专业版身份                 则禁止登陆
                    if ( $getData['professional'] == 1 && $userInfo['fProfessional'] != 1 )
                    {
                        $this->auto_error('您的账号暂未开通专业版使用权限, 如需开通请联系当地服务商!');
                    }

                    //  如果不是专业版的APP, 但是是专业版的身份              则禁止登陆
                    if( $getData['professional'] != 1 && $userInfo['fProfessional'] == 1 )
                    {
                        $this->auto_error('您目前为专业版账号，请使用[熊猫专业版]APP登录!');
                    }

                }

            }
            //ios版本低于34，限制专业版登陆,34版本已具有专业版功能
            if ( $getData['bmxSource'] == 'ios' && $getData['buildVersion'] < '34' && $userInfo['fProfessional'] == 1) {
                $this->auto_error('当前APP版本暂不支持专业版用户登陆！');
            }

        }


        //用户注册登录行为记录
        ( new \Home\Server\UserBehaviorService() ) ->Behavior_Record( $userInfo['fUserId'], 'login', $getData['bmxSource'], $getData['bmxVersion'], $data['user_ip'], $data['longitude'], $data['latitude'], $data['city'] );

		$this->output(1,$userInfo);
	}

	/**
	 * @desc: 站点注册接口
	 * @Author: delayhsiao
     * @again quyang 2017-1-17
	 * @update: 2016-11-21
	 **/
	public function register(){
		$data = I('param.');
		if(empty($data['mobile']) || empty($data['password'])){
			$this->param_error();
		}
		$repeat = D('Home/User')->checkMobileRepeat($data['mobile']);
		if(!$repeat){
			$this->auto_error('手机号重复！');
		}

        if(!preg_match('/^[1][34578]\d{9}$/', $data['mobile'])){
            $this->auto_error('手机号错误');
        }

        $insert = array(
            'f_user_name' => $data['userName'] ? $data['userName'] : $data['mobile'],
            'f_user_pwd' => $data['password'],
            'f_user_mobile' => $data['mobile'],
            'f_user_type_id' => empty($data['userType'])?0:$data['userType'],
            'f_user_status' => '1',
            'f_user_phone'=>$data['mobile'],
            'f_bond' => '1',
            'f_create_time' => date('YmdHis',time()),
            'f_register_platform' => empty( $data['f_register_platform'] ) ? 2 : $data['f_register_platform'], //兼容享买系统  1.为享买注册  默认包裹系统注册  2017-4-24 quyang
            'f_create_timetamp'=>time()
        );

        if(!empty($data['invitCode'])) {
            $proxyId = D('Home/User')->where(array('f_invit_code' => $data['invitCode']))->getField('f_user_id');
            if(!empty($proxyId)) {
                $insert['f_proxy_id'] = $proxyId;
            }
        }
		$result = D('tp_user')->add($insert);
		if($result){
			//删除f_station_id  1-19
			$userInfo = D('tp_user')->where(array('f_user_id'=>$result))->field('f_user_id as fUserId,f_user_pwd as fUserPwd,f_user_mobile as fUserMobile,f_user_name as userName')->find();

            //get参数
            $getData = I('get.');

            //用户注册登录行为记录
            ( new \Home\Server\UserBehaviorService() ) ->Behavior_Record( $result, 'register', $getData['bmxSource'], $getData['bmxVersion'], $data['user_ip'], $data['longitude'], $data['latitude'], $data['city'] );

            $this->output(true, $userInfo);
		}else{
			$this->output(false);
		}
	}

    /**
     * @desc:  快递员注册
     * @Author: zhoutai
     * @again quyang 2017-1-17
     * @update: 16/11/22
     **/

    public function courierRegister(){
        $receive = I('param.');
        if (empty($receive['mobile']) || empty($receive['password']) || empty($receive['realName']) || empty($receive['companyId']) || empty($receive['companyName']) || empty($receive['areaId']) || empty($receive['address'])){
            $this->param_error();
        }
        $user = D('tp_user');
        $repeat = $user->where(array('f_user_mobile' => $receive['mobile']))->find();
        if(!empty($repeat)) $this->auto_error('手机号已存在');

        if(!preg_match('/^[1][34578]\d{9}$/', $receive['mobile'])){
            $this->auto_error('手机号错误');
        }

        //老版本必传参数和新版本必传参数都没传则为非法请求
        if ( empty($receive['privinceId'] ) && empty($receive['region']) )
        {
            $this->param_error();
        }

        //app定位地址信息不好传递
        //地址信息以，分隔  切割并获取省市信息
        if ( empty($receive['privinceId'] ) && !empty($receive['region'])){

            $region_data = explode( ',' , $receive['region'] );

            $receive['privinceName'] = $region_data['0'];
            $receive['cityName'] = $region_data['1'];
            $receive['areaName'] = $region_data['2'];

            //根据app区域id匹配省市id
            $Area = new \Home\Model\AreaModel();
            $list = $Area->getRegion( ['a.f_id'=>$receive['areaId']] );

            $receive['cityId'] = $list['cityId'];
            $receive['privinceId'] = $list['provinceId'];

        }

        $userData = array(
            'f_user_name' => $receive['mobile'],
            'f_user_pwd'  => $receive['password'],
            'f_user_mobile' => $receive['mobile'],
            'f_user_phone'  => $receive['mobile'],
            'f_create_time' => date('YmdHis', time()),
            'f_create_timetamp'=>time(),
            'f_province_id' => empty($receive['privinceId']) ? 0 : $receive['privinceId'],
            'f_city_id'  => empty($receive['cityId']) ? 0 : $receive['cityId'],
            'f_area_id'  => $receive['areaId'],
            'f_address' => $receive['address'],
            'f_province_name' => $receive['privinceName'],
            'f_city_name'   => $receive['cityName'],
            'f_area_name'   => $receive['areaName'],
            'f_user_status' => '1',
            'f_bond' => '1',
            'f_user_type_id' => '1',
            'f_user_real_name'  => $receive['realName'],
            'f_addition_status' => '1',
            'f_addition_create_time' => date('YmdHis', time()),
            'f_addition_type' => '0',
            'f_company_id' => $receive['companyId'],
            'f_company_name' => $receive['companyName']
        );
        if ($receive['f_user_email'] != ''){
            $userData['f_user_email'] = $receive['f_user_email'];
        }
        $insertId = $user->data($userData)->add();

        //get参数
        $getData = I('get.');

        //用户注册登录行为记录
        ( new \Home\Server\UserBehaviorService() ) ->Behavior_Record( $insertId, 'register', $getData['bmxSource'], $getData['bmxVersion'], $receive['user_ip'], $receive['longitude'], $receive['latitude'], $receive['city'] );

        //返回值可以去掉
        $this->output(1, array('fUserId'   => $insertId, 'fUserMobile' => $receive['mobile'], 'fUserPwd'  => $receive['password']));
    }

    /**
     * @desc: 用户设置短信微信开关接口
     * @Author: delayhsiao
     * @update: 2016-11-23
     * @again quyang 2017-2-4 短信微信开关弃用
     **/
    public function updateStationInfo(){
		$data = I('param.');
		if($data['updateType'] == '' || empty($data['stationId'])){
			$this->param_error();
		}
		switch($data['updateType']){
			case 0:
                $this->output(true);
				break;
			case 1:
				if(empty($data['storeTime'])){
					$this->output(false);
				}
				break;
		}
		$result = D('Home/User')->updateStationInfo($data);
		if($result){

		    $server = new \Home\Server\ParcelService();
		    $server->delRedis_UserInfo( $data['stationId'] );

			$this->output(true);
		}else{
			$this->output(false);
		}
	}

	/**
	 * @desc: 提交站点审核信息
	 * @Author: delayhsiao
     * @again quyang 2017-1-17
	 * @update: 2016-11-23
	 **/
	public function verifyStation(){
		$data = I('param.');
		if(empty($data['realName']) || empty($data['idNo'])  || empty($data['areaId']) || empty($data['address']) || empty($data['imgUrls']) || empty($data['userId']) || empty($data['shopName'])){
			$this->param_error(false);
		}

		//查找注册信息是否存在
		$repeat = D('Home/User')->where(array('f_user_id'=>$data['userId']))->field('f_user_id,f_proxy_id,f_city_id,f_area_id,f_addition_status')->find();
		if(empty($repeat['f_user_id'])){
			$this->auto_error('未找到注册信息');
		}

		//防止支付成功再次填写审核资料
        if ($repeat['f_city_id'] > 0){
		    $this->auto_error('信息已提交，请勿重复提交');
        }

        //老版本必传参数和新版本必传参数都没传则为非法请求
        if ( empty($data['provinceId'] ) && empty($data['region']) )
        {
            $this->param_error();
        }

        //屏蔽快递公司关键词
        $Verification_stationName = array(
            '菜鸟','快递','顺丰','中通','申通','圆通','百世','韵达','天天','咕啦','淘宝','天猫','京东','收货宝','圆通妈妈','苏宁','唯品会','嘿客','丰巢','优速','如风达','宅急送','邮政','德邦'
        );

        foreach ( $Verification_stationName as $value )
        {

            if ( stristr( $data['shopName'] , $value ) )
            {
                $this->auto_error( '店铺名称不合法' );
            }

        }

        //app定位地址信息不好传递
        //地址信息以，分隔  切割并获取省市信息
        if ( empty($data['provinceId'] ) && !empty($data['region'])){

            $region_data = explode( ',' , $data['region'] );

            //根据app区域id匹配省市id
            $Area = new \Home\Model\AreaModel();
            $list = $Area->getRegion( ['a.f_id'=>$data['areaId']] );

            $data['cityId'] = $list['cityId'];
            $data['provinceId'] = $list['provinceId'];
            $data['provinceName'] = $region_data['0'];
            $data['cityName'] = $region_data['1'];
            $data['areaName'] = $region_data['2'];

        }

		//兼容旧版本  以 | 分隔
        $ex_url = explode('|',$data['imgUrls']);
		if ( !empty( $ex_url ) ){
            $data['imgUrls'] = implode( ',',  $ex_url);
        }

        //查询系统模板
        $SmsTemplate = new \Home\Model\SmsTemplateModel();
		$templateId = $SmsTemplate->getemplate( '' );

		//附件表
        $url = $data['imgUrls'];
        //审核信息表
        $addition = array(
            'f_user_id' => $data['userId'],
            'f_user_real_name' => $data['realName'],
            'f_user_crad_no' => $data['idNo'],
            'f_main_business' => empty($data['mainBusiness'])?'':$data['mainBusiness'],
            'f_addition_create_time' => date('YmdHis',time()),
            'f_addition_status' => intval( $repeat['f_addition_status'] ),
            'f_addition_type' => '0',
//            'f_store_time' => empty($data['time'])?'':$data['time'],
            'f_station_name' => $data['shopName'],
            'f_store_time' => '早9点至晚8点',
            'f_url' => $url,
            'f_province_id' => empty($data['provinceId']) ? 0 : $data['provinceId'],
            'f_city_id' => empty($data['cityId']) ? 0 : $data['cityId'],
            'f_area_id' => $data['areaId'],
            'f_province_name' => $data['provinceName'],
            'f_city_name' => $data['cityName'],
            'f_area_name' => $data['areaName'],
            'f_address' => $data['address'],
            'f_template_id'=>empty( $templateId['f_template_id'] ) ? 0 : $templateId['f_template_id']
        );

        //如果用户没有填邀请码，也就没有上级代理ID。  则根据地区自动归属代理ID
        $proxy_id = intval( $repeat['f_proxy_id'] );

        if( $proxy_id == 0 )
        {
            //获取代理账号信息
            $proxyData = ( new \Think\Model() ) ->table('tp_proxy_info')
                ->field('`f_user_id`, `f_proxy_type`')
                ->where("( `f_city_id` = '{$data['cityId']}' AND `f_proxy_type` = 1 ) OR ( `f_area_id` = '{$data['areaId']}' AND `f_proxy_type` = 2 )")
                ->order('`f_proxy_type` DESC,`f_user_id` DESC')
                ->select()
            ;

            $cityData = $areaData = Array();
            //分组， 如果重复区域， 将市级 和 区级 分组
            foreach( $proxyData as $PVal )
            {
                if( $PVal['f_proxy_type'] == 1 )
                {
                    $cityData[] = $PVal['f_user_id'];
                }

                if( $PVal['f_proxy_type'] == 2 )
                {
                    $areaData[] = $PVal['f_user_id'];
                }
            }

            //如果区级只有一个， 匹配区级代理
            if( count( $areaData ) == 1 )
            {
                $addition['f_proxy_id'] = $areaData[0];
            }

            //如果没有区级, 并且市级只有一个， 匹配市级
            if( count( $areaData ) == 0 && count( $cityData ) == 1 )
            {
                $addition['f_proxy_id'] = $cityData[0];
            }
        }


        $additionResult = D('tp_user')->save($addition);

		if($additionResult){
			//返回参数可删，向上兼容
			$this->output(true,array('verifyId'=>'0'));
		}else{
            $this->auto_error('填写审核资料失败');
		}
	}

    /**
     * @desc:  添加自定义模板接口
     * @Author: zhoutai
     * @update: 16/11/24
     **/

    public function customSmsTemp(){
        $data = I('param.');
        if (empty($data['fTemplateContent']) || empty($data['fTemplateCreateUser'])) $this->param_error();
        $smsTable = D('tp_sms_template');
        $update['f_template_content'] = $data['fTemplateContent'];
        $update['f_template_status'] = 0;
        $update['f_template_create_time'] = date('Y-m-d', time());
        $update['f_template_since'] = 4;
        $update['f_template_create_user'] = $data['fTemplateCreateUser'];
        $update['f_template_type'] = 0;
        $update['f_isGol'] = 0;
        $result = $smsTable->add($update);
        if ($result !== false){
            unset($update['f_template_create_time']);
            $this->output(1, $update);
        }
    }

    /**
     * @desc:  删除自定义模板
     * @Author: zhoutai
     * @update: 16/11/24
     * @again quyang 
     **/

    public function deleteSmsTemp(){
        $data = I('param.');
        if (empty($data['fTemplateId'])) $this->param_error();

        //验证是否是系统模板，否者无法删除
        $where = "f_template_id = {$data['fTemplateId']} AND f_template_since=0 and f_isGol=1 and f_template_status=1";
        $template_Data = D('Home/SmsTemplate')->where( $where )->field('f_template_id')->find();

        if ($template_Data){
            $this->auto_error( '系统模板无法删除' );
        }

        $result = D('tp_sms_template')->where(array('f_template_id' => $data['fTemplateId']))->delete();
        if ($result){
            //判断删除的是否是  默认模板
            $templateId = D('Home/User')->where(array('f_template_id' => $data['fTemplateId']))->field('f_template_id,f_user_id')->find();
            if ( $templateId['f_template_id'] == $data['fTemplateId'] ){

                //查询系统模板
                $SmsTemplate = new \Home\Model\SmsTemplateModel();
                $templateId = $SmsTemplate->getemplate( '' );

                $data = D('Home/User')->where(array('f_template_id' => $data['fTemplateId']))->save(array('f_template_id'=>$templateId['f_template_id']));
                if ($data === false){
                    $this->output(false);
                }

                //删除redis
                $server = new \Home\Server\ParcelService();
                $server->delRedis_UserInfo( $templateId['f_user_id'] );

            }

            $this->output(1);
        }
    }

    /**
     * @desc: 编辑自定义模板
     * @Author: delayhsiao
     * @update: 2016-11-24
     **/
    public function editSmsTemp(){
        $data = I('param.');
        if (empty($data['fTemplateId']) || empty($data['fTemplateContent']) ) $this->param_error();
        $smsTemp = D('tp_sms_template')->where(array('f_template_id' => $data['fTemplateId']))->find();
        if (empty($smsTemp)) $this->auto_error('模板不存在');

        //验证是否是系统模板，否者无法编辑
        $where = "f_template_id = {$data['fTemplateId']} AND f_template_since=0 and f_isGol=1 and f_template_status=1";
        $template_Data = D('Home/SmsTemplate')->where( $where )->field('f_template_id')->find();

        if ($template_Data){
            $this->auto_error( '系统模板无法编辑' );
        }

        $sms_map['f_template_content'] = $data['fTemplateContent'];
        $sms_map['f_template_status'] = '0';
        $isSuccess = D('tp_sms_template')->where(array('f_template_id' => $data['fTemplateId']))->save($sms_map);

        //验证是默认模板则改为系统模板
        $user_map['f_user_id'] = $smsTemp['f_template_create_user'];
        $templateId = D('Home/User')->where( $user_map )->field('f_template_id,f_user_id')->find();
        if ( $data['fTemplateId'] == $templateId['f_template_id'] )
        {
            $SmsTemplate = new \Home\Model\SmsTemplateModel();
            $default = $SmsTemplate->getemplate( '' );
            D('Home/User')->where( $user_map )->save( ['f_template_id'=>$default['f_template_id']] );

            //删除redis
            $server = new \Home\Server\ParcelService();
            $server->delRedis_UserInfo( $templateId['f_user_id'] );

        }

        if (!$isSuccess){
            $this->output(false);
        }
        $this->output(true);
    }

    /**
     * @desc: 设置默认模板
     * @Author: delayhsiao
     * @again quyang 2017-1-17
     * @update: 2016-11-24
     **/
    public function changeUserTemplate(){
        $data = I('param.');
        if (empty($data['userId']) || empty($data['templateId'])) $this->param_error(false);
        $return = D('Home/User')->save(array('f_user_id' => $data['userId'], 'f_template_id' => $data['templateId']));
        if ($return === false){
            $this->output(false);
        }

        //修改redis
        $server = new \Home\Server\ParcelService();
        $server->delRedis_UserInfo( $data['userId'] );

        $this->output(true);
    }

    /**
     * @desc: 获取短信模板列表
     * @Author: delayhsiao
     * @update: 2016-11-26
     **/
    public function querySmsTempById(){
    	$data = I('param.');
    	if(empty($data['fTemplateCreateUser'])){
    		$this->param_error();
		}
		$templateList = D('Home/SmsTemplate')->getSmsTempList($data['fTemplateCreateUser']);
		$this->output(true,$templateList);
	}

	/**
	 * @desc 修改用户信息和审核信息接口
	 * @author quyang
     * @again quyang 2017-1-17
	 * @update 2016-11-23
	 */
	public function updateUserInfo(){
	    $data = I('param.');
	    if(empty($data['userId'])){
	        $this->param_error();
	    }
	    $where = array('f_user_id'=>$data['userId']);

	    if (!empty($data['privinceId']) && !empty($data['cityId']) && !empty($data['areaId']) && !empty($data['privinceName']) && !empty($data['cityName']) && !empty($data['areaName'])){
            $map = array(
                'f_province_id'=>$data['privinceId'],
                'f_city_id'=>$data['cityId'],
                'f_area_id'=>$data['areaId'],
                'f_province_name'=>$data['privinceName'],
                'f_city_name'=>$data['cityName'],
                'f_area_name'=>$data['areaName']
            );
        }

        !empty($data['address']) && $map['f_address'] = $data['address'];

        if (!empty($data['courierName'])){
	        $map['f_user_real_name'] = $data['courierName'];
        }

	    $arr = D('Home/User')->where($where)->save($map);

	    if ($arr === false){
            $this->output(false);
        }

        //修改redis
        $server = new \Home\Server\ParcelService();
        $server->delRedis_UserInfo( $data['userId'] );

	    $list['userAddressBean']['fAddress'] = $data['address'];
	    $list['userAddressBean']['fAreaId'] = $data['areaId'];
	    $list['userAddressBean']['fAreaName'] = $data['areaName'];
	    $list['userAddressBean']['fCityId'] = $data['cityId'];
	    $list['userAddressBean']['fCityName'] = $data['cityName'];
	    $list['userAddressBean']['fPrivinceId'] = $data['privinceId'];
	    $list['userAddressBean']['fPrivinceName'] = $data['privinceName'];
	    $list['verifyBean']['userRealName'] = $data['courierName'];
	    $this->output(1,$list);
	}

	/**
     * @desc 记录用户退出log（临时）
     * @author quyang
     * @update 2016-12-26
     */
	public function userLogout(){
	    $param = I('param.');
        if (empty($param['userId']) || empty($param['taskId'])){
            $this->param_error();
        }
        $arr = D('tp_app_logout_log')->where(array('f_object'=>$param['userId']))->find();
        if (!empty($arr)){
            $where = array('f_object'=>$param['userId']);
            D('tp_app_logout_log')->where($where)->save(array('f_state'=>'1'));
            $this->output(1);
        }
        $map = array('f_type'=>'1','f_object'=>$param['userId'],'f_state'=>'1','f_task_id'=>$param['taskId'],'f_creat_time'=>date('YmdHis'));
        D('tp_app_logout_log')->add($map);
        $this->output(1);
    }

    /**
     * @desc 查询用户是否退出升级（临时）
     * @author quyang
     * @update 2016-12-26
     */
	public function queryUpdate(){
	    $param = I('param.');
	    if (empty($param['userId']) || empty($param['loginTime'])){
	        $this->param_error();
        }
        $map['_string'] = "(f_type = 0 OR (f_type = 1 AND f_object=".$param['userId'].")) AND f_creat_time > ".$param['loginTime'];
        $arr = D('tp_app_logout_log')->where($map)->order('f_creat_time desc')->find();
        if (empty($arr)){
            $this->output(true,"no");
        }else{
        	if($arr['f_type']==0){
        		$log = D('tp_app_logout_log')->where(array('f_object'=>$param['userId'],'f_task_id'=>$arr['f_task_id']))->getField('f_state');
        		switch($log){
					case '':
					case 0:
						$this->output(true,$arr['f_task_id']);
						break;
					case 1:
						$this->output(true,"no");
						break;
				}
			}else{
        		$this->output(true,$arr['f_state']==0?$arr['f_task_id']:"no");
			}
		}
    }
    
    /**
     * 修改密码或用户名
     * @param type $data['userId'] 用户id
     * @param type $data['oldPwd'] 旧密码
     * @param type $data['newPwd'] 新密码
     * @param type $data['userName'] 用户名
     */
    public function modifyPwd() {
        $data=I('param.');
        if(empty($data['userId']) || empty($data['oldPwd']) || empty($data['newPwd'])){
            $this->param_error();
        }
        $map['f_user_id'] = $data['userId'];
        $map['f_user_pwd'] = $data['oldPwd'];
        $info = D('Home/User')->where($map)->getField('f_user_id');
        if ($info) {
            $data['f_user_pwd'] = $data['newPwd'];
            !empty($data['userName']) && $data['f_user_name'] = $data['userName'];
            D('Home/User')->where($map)->save($data);
            $this->output(1);
        } else {
            $this->output(0);
        }
    }

    /**
     * @desc: 根据用户id修改审核状态为通过
     * @param type $uid 用户id
     * @Author: cheng
     **/
    public function saveStatus($uid){
        $result = $this->where(['f_user_id'=>$uid])->save(['f_addition_status' => 1, 'f_addition_time' => date('YmdHis')]); //审核通过
        return $result;
    }


    /**
     * Function setSuperPwd
     *
     * Author: 黄盖
     * Date: 2017-3-21 15:13
     * Info: 超级密码设置修改
     */
    public function setSuperPwd()
    {
        $data = I('param.');

        $superPwd = trim( $data['superPwd'] );

        //超级密码规则验证
        if( ! preg_match( "/^[\d+]{8}$/", $superPwd ) || in_array( $superPwd, $this ->SuperPwd_Rule() ) )
        {
            $this->auto_error('超级密码必须是8位数字, 且不能相同!');
        }

        if( ( new \Home\Server\UserService() ) ->setUserInfo( $data['userID'], Array( 'f_super_password' => $superPwd ) ) )
        {
            $this ->output(1, '设置成功');
        }

        $this->output(0);
    }

    /**
     * Function SuperPwd_Rule
     * @return array
     *
     * Author: 黄盖
     * Date: 2017-4-11 11:00
     * Info: 超级密码 禁止使用如下内容
     */
    public function SuperPwd_Rule()
    {
        return Array(
                    '00000000',
                    '11111111',
                    '22222222',
                    '33333333',
                    '44444444',
                    '55555555',
                    '66666666',
                    '77777777',
                    '88888888',
                    '99999999',
            );
    }

    /**
     * @return json
     *
     * Author: 易风
     * Date: 2017-5-26 10:00
     * Info: 获取站点
     * @param pasNum 跳过记录数
     * @param perNum 每页记录数
     * @param 查询条件（自定义）
     */
    public function getStations(){
        $data = I('param.');
        unset($data['reqUrl'], $data['pasNum'], $data['perNum']);
        $info = D('tp_user')->field('f_user_id,f_station_name,f_address')->where($data)->where(['f_user_status'=>1,'f_state'=>1,'f_addition_status'=>1])->order('f_user_id','ASC')->limit(I('param.')['pasNum'],I('param.')['perNum'])->select();
        if($info)
            exit(json_encode(['status'=>0,'info'=>$info,'msg'=>'查到记录']));
        else
            exit(json_encode(['status'=>1,'info'=>$info,'msg'=>'未查到记录']));
    }

    /**
     * 设置、获取包裹编号是否重复状态
     * 参数：
     * 0 查询站点的状态 ；
     * 非零 ：1 编号可重复 ； 2 编号不重复 ；
     */
    public function setPacketNumRepeatStatus(){
        $data = I('param.');
        if (empty($data['stationId']))
        {
            $this -> param_error();
        }

        // 包裹编号是否重复： 0 查询站点的状态 ；非零 ：1 编号可重复 ； 2 编号不重复 ；
        if (empty($data['packetnumRepeat']) || $data['packetnumRepeat'] == '')
        {
            $return = D('Home/User') -> where(array('f_user_id' => $data['stationId'])) -> getField('f_packetnum_repeat');

            $res['packetnumRepeat'] = $return;

            $this -> output(true,$res);
        }
        else
        {
            $return = D('Home/User') -> where(array('f_user_id' => $data['stationId'])) -> getField('f_packetnum_repeat');

            // 请求修改的数据与原数据相同，直接返回
            if ($return == $data['packetnumRepeat'])
            {
                $res['packetnumRepeat'] = $data['packetnumRepeat'];

                $this -> output(true,$res );
            }
            else
            {
                $where['f_packetnum_repeat'] = $data['packetnumRepeat'];

                $return = D('Home/User') -> where(array('f_user_id' => $data['stationId'])) ->save($where);

                $res['packetnumRepeat'] = $data['packetnumRepeat'];

                $this -> output(true,$res );
            }

        }

    }

    /**
     * 获取api接口类型
     */
    public function getApiType()
    {
        $param = I('param.');

        if ( empty($param['stationId']))
        {
            $this -> param_error();
        }

        $user = new \Home\Model\UserModel();

        $api_type = $user -> where(array('f_user_id' => $param['stationId'])) -> field('f_api_type') -> find();

        if ($api_type)
        {
            $this -> output(true,$api_type);
        }
        else
        {
            $this -> output(false);
        }

    }
}
